package com.ss.android.article.browser.setting;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.CookieManager;
import android.widget.TextView;

import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.video.cache.VideoCacheController;
import com.ss.android.article.browser.R;
import com.ss.android.article.browser.database.b.HistoryManager;
import com.ss.android.article.browser.lockscreen.ui.SettingLockScreenActivity;
import com.ss.android.article.browser.preference.PreferenceManager;
import com.ss.android.b.b.BaseVideoManager;
import com.ss.android.common.app.IComponent;
import com.ss.android.common.app.permission.PermissionsManager;
import com.ss.android.common.app.permission.PermissionsResultAction;
import com.ss.android.common.applog.AppLog;
import com.ss.android.common.dialog.AlertDialog;
import com.ss.android.common.ui.view.SwitchButton;
import com.ss.android.image.BaseImageManager;
import com.ss.android.image.FrescoUtils;
import com.ss.android.newmedia.feedback.FeedbackActivity;

import java.lang.ref.WeakReference;

public class SettingActivity extends Activity implements Callback, com.ss.android.article.base.feature.d.a.a$a, IComponent {
    public static final int[] a;
    static final int[] b;
    private final WeakHandler c;
    private TextView d;
    private TextView e;
    private int f;
    private int g;
    private PreferenceManager mPreferenceManager;
    private SwitchButton i;
    private com.ss.android.article.base.feature.d.a j;
    private boolean k;
    private boolean l;
    private String m;
    private AppData mAppData;
    private TextView o;
    private View p;
    private WeakReference q;
    private TextView r;
    private TextView s;
    private TextView t;
    private long u;
    private int v;
    private boolean w;
    private View.OnClickListener x;
    private SwitchButton y;
    private View z;

    static {
        a = new int[]{1, 2, 4};
        b = new int[]{1, 0, 2, 3};
    }

    public SettingActivity() {

        this.c = new WeakHandler(((Callback) this));
        this.k = false;
        this.l = false;
        this.m = "1.0";
        this.q = null;
        this.u = 0;
        this.v = 0;
        this.w = false;
        this.x = new e(this);
    }

    static int a(SettingActivity arg0, int arg1) {
        arg0.f = arg1;
        return arg1;
    }

    static long a(SettingActivity arg1, long arg2) {
        arg1.u = arg2;
        return arg2;
    }

    static boolean a(SettingActivity arg1) {
        return arg1.w;
    }

    public final void a() {
    }

    public final void a(boolean arg6) {
        int v4 = -1;
        Dialog v1 = new Dialog(((Context) this), R.style.ee);
        LayoutInflater v2 = LayoutInflater.from(((Context) this));
        int v0 = arg6 ? R.layout.cq : R.layout.cp;
        View v0_1 = v2.inflate(v0, null);
        v0_1.findViewById(com.ss.android.article.browser.R.id.rn).setOnClickListener(new q(this, v1, arg6));
        v1.addContentView(v0_1, new ViewGroup.LayoutParams(v4, v4));
        Window v0_2 = v1.getWindow();
        WindowManager.LayoutParams v2_1 = v0_2.getAttributes();
        v2_1.width = v4;
        v2_1.height = v4;
        v0_2.setAttributes(v2_1);
        v1.show();
    }

    static int b(SettingActivity arg0, int arg1) {
        arg0.g = arg1;
        return arg1;
    }

    static long b(SettingActivity arg2) {
        return arg2.u;
    }

    static void b(boolean arg2) {
        com.ss.android.newmedia.message.c.a().a(Boolean.valueOf(arg2));
    }

    public final void b() {
    }

    static int c(SettingActivity arg2) {
        int v0 = arg2.v;
        arg2.v = v0 + 1;
        return v0;
    }

    private String c() {
        String v0 = this.mAppData == null || this.mAppData.aC() == null ? null : this.mAppData.aC()
                + "\nuid: " + Spipe.inst().userId
                + "\ndevice_id: " + AppLog.deviceId()
                + "\nuser_city: " + this.mAppData.localCityName
                + "\ncurrent_city: " + this.mAppData.currentCityName
                + "\nmanifest_version: " + this.mAppData.getAppContext().getManifestVersion()
                + "\napi_version: " + this.mAppData.getAppContext().getVersionCode();
        return v0;
    }

    static void c(SettingActivity arg2, int arg3) {
        if (arg3 != 0) {
            arg2.q = new WeakReference(com.ss.android.j.b.createDialogBuilder(((Context) arg2)).setTitle(com.ss.android.article.browser.R.string.tip).setMessage(com.ss.android.article.browser.R.string.clearing_cache).setCancelable(false).show());
            new g(arg2, arg3).start();
        }
    }

    static int d(SettingActivity arg1) {
        arg1.v = 1;
        return 1;
    }

    private void d() {
        this.mAppData = AppData.getInstance();
        this.m = this.mAppData.getAppContext().getAppVersion();
        this.mPreferenceManager = PreferenceManager.getInstance();
        this.j = new com.ss.android.article.base.feature.d.a(((Context) this), ((IComponent) this), this);
        this.g = this.mPreferenceManager.getTextSize();
        AppData.getInstance().a(SettingActivity.b[this.mPreferenceManager.getTextSize()]);
        this.f = this.mPreferenceManager.getSearch();
        this.d.setText(this.getResources().getStringArray(R.array.k)[this.f]);
        this.e.setText(this.getResources().getStringArray(R.array.m)[this.g]);
        this.i.setChecked(com.ss.android.newmedia.message.c.a().h());
        try {
            this.o.setText(this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName);
        } catch (Exception v0) {
            v0.printStackTrace();
            if (TextUtils.isEmpty(this.m)) {
            } else {
                this.o.setText(this.m);
            }
        }

        if (Spipe.inst().isLogin) {
            this.p.setVisibility(View.VISIBLE);
        } else {
            this.p.setVisibility(View.GONE);
        }

        this.m = this.mAppData.getAppContext().getAppVersion();
        if (android.support.design.a.isTextEmpty(this.m)) {
            this.m = "1.0";
        }

        String v0_1 = this.mAppData.aC();
        if (android.support.design.a.isTest(this.getApplicationContext())) {
            v0_1 = this.c();
            this.w = true;
        }

        this.t.setText(((CharSequence) v0_1));
    }

    static void d(SettingActivity arg2, int arg3) {
        if ((arg3 & 1) == 1) {
            HistoryManager.getInstance().b();
        }

        if ((arg3 & 4) == 4) {
            new BaseImageManager(((Context) arg2)).c();
            new BaseVideoManager(((Context) arg2)).a();
            VideoCacheController.getInstance().clearCache();
            FrescoUtils.clearCache();
            SharedPreferences.Editor v0 = arg2.mPreferenceManager.mSharedPreferences.edit();
            v0.clear();
            v0.apply();
        }

        if ((arg3 & 2) == 2) {
            CookieManager.getInstance().removeAllCookie();
        }
    }

    static int e(SettingActivity arg1) {
        return arg1.v;
    }

    static String f(SettingActivity arg1) {
        return arg1.c();
    }

    static TextView g(SettingActivity arg1) {
        return arg1.t;
    }

    static boolean h(SettingActivity arg1) {
        arg1.w = true;
        return true;
    }

    public void handleMsg(Message message) {
        Object v0 = null;
        if (this.q != null) {
            v0 = this.q.get();
        }

        if (v0 != null) {
            ((AlertDialog) v0).dismiss();
        }

        switch (message.what) {
            case 4: {
                UIUtils.showImageToast(((Context) this), R.drawable.doneicon_popup_textpage, R.string.toast_finish_clear);
                this.d();
                break;
            }
        }
    }

    static View i(SettingActivity arg1) {
        return arg1.p;
    }

    public boolean isActive() {
        return this.k;
    }

    public boolean isViewValid() {
        boolean v0 = !this.l ? true : false;
        return v0;
    }

    static TextView j(SettingActivity arg1) {
        return arg1.d;
    }

    static PreferenceManager k(SettingActivity arg1) {
        return arg1.mPreferenceManager;
    }

    static TextView l(SettingActivity arg1) {
        return arg1.e;
    }

    static void sendModifyFontSizeBroadcast(SettingActivity settingActivity) {
        LocalBroadcastManager.getInstance(settingActivity.getApplication())
                .sendBroadcast(new Intent("com.ss.android.article.browser.modifyFontSize"));
    }

    static WeakHandler n(SettingActivity arg1) {
        return arg1.c;
    }

    public void onBackCLick(View view) {
        this.finish();
    }

    public void onCheckUpdateClick(View view) {
        PermissionsManager.getInstance().requestPermission(this,
                new String[]{"android.permission.WRITE_EXTERNAL_STORAGE"},
                new PermissionsResultAction() {


                    public final void onGranted() {
                        SettingActivity.this.j.b();
                    }

                    public final void onDenied(String s) {
                    }
                });
    }

    public void onClearDateClick(View view) {
        new r(((Context) this), this.getResources().getString(com.ss.android.article.browser.R.string.clear_data), this.getResources().getStringArray(R.array.j), new f(this)).show();
        new com.ss.android.g.a.a$a("click_clear_cache").a().a();
    }

    protected void onCreate(@Nullable Bundle arg4) {
        super.onCreate(arg4);
        this.setContentView(com.ss.android.article.browser.R.layout.i7);
        this.findViewById(com.ss.android.article.browser.R.id.a_x);
        this.findViewById(com.ss.android.article.browser.R.id.a_z);
        this.findViewById(com.ss.android.article.browser.R.id.aa1);
        this.findViewById(com.ss.android.article.browser.R.id.aa1);
        this.findViewById(com.ss.android.article.browser.R.id.aa4);
        this.findViewById(com.ss.android.article.browser.R.id.aa5);
        this.findViewById(com.ss.android.article.browser.R.id.aa6);
        this.e = this.findViewById(com.ss.android.article.browser.R.id.aa0);
        this.d = this.findViewById(com.ss.android.article.browser.R.id.a_y);
        this.i = this.findViewById(com.ss.android.article.browser.R.id.a_g);
        this.i.setChecked(com.ss.android.newmedia.message.c.a().h());
        this.p = this.findViewById(com.ss.android.article.browser.R.id.a_q);
        this.i.setOnCheckStateChangeListener(new j());
        this.findViewById(com.ss.android.article.browser.R.id.a_w);
        if (android.support.design.a.isTest(((Context) this))) {
            View v0 = this.findViewById(com.ss.android.article.browser.R.id.i2);
            UIUtils.setVisibility(v0, 0);
            v0.setOnClickListener(new p(this));
        }

        this.o = this.findViewById(com.ss.android.article.browser.R.id.a_m);
        this.p.setOnClickListener(new k(this));
        this.r = this.findViewById(com.ss.android.article.browser.R.id.a_t);
        this.r.setOnClickListener(new l(this));
        this.s = this.findViewById(com.ss.android.article.browser.R.id.a_u);
        this.s.setOnClickListener(this.x);
        this.t = this.findViewById(com.ss.android.article.browser.R.id.a_v);
        this.y = this.findViewById(com.ss.android.article.browser.R.id.aa2);
        this.y.setOnCheckStateChangeListener(new m(this));
        this.z = this.findViewById(com.ss.android.article.browser.R.id.aa3);
        if (!AppData.getInstance().getAppSettings().getLockScreenEnable() || !com.ss.android.article.browser.lockscreen.e.b().a()) {
            this.z.setVisibility(View.GONE);
        } else {
            this.z.setVisibility(View.VISIBLE);
        }

        this.d();
    }

    protected void onDestroy() {
        super.onDestroy();
        this.l = true;
    }

    public void onFeedBackClick(View arg3) {
        this.startActivity(new Intent(((Context) this), FeedbackActivity.class));
        new com.ss.android.g.a.a$a("click_feedback").a().a();
    }

    public void onFontSizeClick(View arg8) {
        new r(this, this.getResources().getString(com.ss.android.article.browser.R.string.gk), this.getResources().getStringArray(R.array.m), new o(this), 0, this.g).show();
        new com.ss.android.g.a.a$a("click_font").a().a();
    }

    public void onLockScreenSettingClick(View arg3) {
        this.startActivity(new Intent(((Context) this), SettingLockScreenActivity.class));
    }

    public void onNotificationClick(View arg1) {
    }

    protected void onPause() {
        super.onPause();
        this.k = false;
    }

    protected void onResume() {
        boolean v0 = true;
        super.onResume();
        this.k = true;
        SwitchButton v1 = this.y;
        if (android.support.design.a.p(((Context) this)) != DefaultSettingHandler$Status.OK) {
            v0 = false;
        }

        v1.setChecked(v0);
    }

    public void onSearchEngineClick(View arg8) {
        new r(this, this.getResources().getString(com.ss.android.article.browser.R.string.gm), this.getResources().getStringArray(R.array.k), new n(this), 0, this.f).show();
        new com.ss.android.g.a.a$a("click_search_engine").a().a();
    }

    protected void onStop() {
        super.onStop();
        this.k = false;
    }
}

